package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.jpa.TblCampania;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.CampanasPorAutorizarForm;
import com.mastercard.assets.web.util.to.UserLogin;
//import java.util.ArrayList;
//import java.util.Calendar;
//import java.util.GregorianCalendar;

public class CampanasPorAutorizarBuscar
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		// verifica que el rol en el que esta es banco (2)
		int role = user.getActualRoleId( ) ;
		
		if( role != 2 )
			return mapping.findForward( "RoleNoValido" ) ;
		
		// busca campanas del usuario
		// toma el usuario
		int userId = user.getCveUsuario( ) , cvePais = user.getTblUsuario( ).getTblPais( ).getCvePais( ) ;
		
//                List< TblCampania > campanias = MasterCardDAO.findAllActivCampaigns( ) ;
                List< TblCampania > campanias = MasterCardDAO.findAllActivCampaignsByCountry( cvePais ) ;
                
//                String startTime = "", finalTime = "" , trs = "" ;
//                int i = 0 ;
//                for( i = 0 ; i < 200000 ; i++ ) {
//                    campanias = MasterCardDAO.findAllActivCampaigns( ) ;
//                    
//                    Calendar calendario = new GregorianCalendar( ) ;
//                    int hora, minutos, segundos, h, m, s ;
//                    hora =calendario.get(Calendar.HOUR_OF_DAY);minutos = calendario.get(Calendar.MINUTE);
//                    segundos = calendario.get(Calendar.SECOND);
//                    h =calendario.get(Calendar.HOUR_OF_DAY); m = calendario.get(Calendar.MINUTE);
//                    s = calendario.get(Calendar.SECOND);
//                    
//                    System.out.println( "(°_°)==== Transaccion ==== " + i ) ;
//                    System.out.println( "(°_°)==== Usuario ==== " + user.getCveUsuario( ) ) ;
//                    if( i == 0 ) {
//                        System.out.println( "( Inicio )==== Reloj ==== " + hora + ":" + minutos + ":" + segundos ) ;
//                        startTime = ""+hora+":"+minutos+":"+segundos+"" ;
//                    } else
//                        if( i == 199999 ) {
//                            System.out.println( "( Fin )==== Reloj ==== " + hora + ":" + minutos + ":" + segundos ) ;
//                            finalTime = ""+h+":"+m+":"+s+"" ;
//                            System.out.println( "+--------------------------------+") ;
//                            System.out.println( "|----- Inicio:"+startTime+" -----|") ;
//                            System.out.println( "|-----  Final:"+finalTime+" -----|") ;
//                            System.out.println( "+--------------------------------+") ;
//                        }
//                    
//                }
//                trs = "trs: " + i ;
//                System.out.print("trans == " + trs ) ;
//		request.setAttribute("ini", startTime);
//                request.setAttribute("fin", finalTime);
//                request.setAttribute("t", trs ) ;
		
		// en caso d eque no haya campanias
		if( campanias == null || campanias.size( ) == 0 )
		{
                    MasterCardDAO.closeEntityManager( ) ;
			return mapping.findForward( "NoCampanas" ) ;
		}
		
		// forma el formulario
		CampanasPorAutorizarForm f = new CampanasPorAutorizarForm( ) ;
		f.setCampanias( campanias ) ;
		
		request.setAttribute( "CampanasPorAutorizarForm" , f ) ;
		
                MasterCardDAO.closeEntityManager( ) ;
                if( user.getIdioma( )  == 0 ) {
                    return mapping.findForward( "success" ) ;
                } else
                    return mapping.findForward( "successEng" ) ;
		
	}
	
}
